package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Housers;
import com.etc.entity.Users;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.HousersService;
import com.etc.service.impl.HousersServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class AdminsHouseServlet
 */
@WebServlet("/ahs.do")
public class AdminsHouseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private HousersService housersService = new HousersServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminsHouseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//执行封装的分页查询
				String op= "query";
				if (request.getParameter("op")!=null) {
					op=request.getParameter("op");
				}
				if (op.equals("query")) {
					PgeeDoQuery(request, response);
				}
				//执行删除方法
				else if (op.equals("del")) {
					int id =Integer.valueOf(request.getParameter("id"));
					boolean flag= housersService.DelHousers(id);
					//获取到Id后打印
					PrintWriter out = response.getWriter();
					out.print(flag);
					out.close();
				}
				else if (op.equals("update")) {
					//这时候用的是request.getReader();这个方法是用于获取页面一侧的ajax请求中的Json数据
				    BufferedReader reader= request.getReader();
				    //用字符串拼接的方式来拼接接收到的JSON数据
				    StringBuffer sBuffer = new StringBuffer();
				    //定义一个字符串让他为空，在下面的while循环中不断赋予它值，使得字符串可以拼接
				    String str = null;
				    while((str=reader.readLine())!=null) {
				    	sBuffer.append(str);
				    }
				    System.out.println(sBuffer);
				    //将GSON数据转化成Users对象，方便后面传入数据库
				    Gson gson = new Gson();
				    //转换对象的方式，并赋予users
				    Housers housers= gson.fromJson(sBuffer+"",Housers.class);
				    System.out.println(housers);
				    
//				    int id =Integer.parseInt(request.getParameter("id"));
				    
				    boolean flag= housersService.UpdHousers(housers);
				    PrintWriter out = response.getWriter();
					out.print(flag);
//					out.close();
					System.out.println(flag);
				}else if(op.equals("add")) {
					String houseName = request.getParameter("houseName");
					String houseDoorNo = request.getParameter("houseDoorNo");
					String houseType = request.getParameter("houseType");
					int houseState = Integer.valueOf(request.getParameter("houseState"));
					Housers housers = new Housers(houseName, houseDoorNo, houseType, houseState);
					boolean flag = housersService.AddHousers(housers);
					request.getRequestDispatcher("admin_house.jsp").forward(request, response);
					PrintWriter out = response.getWriter();
					out.print(flag);
					out.close();
				}
				else if(op.equals("removesession")) {
					//退出后清除所有的session信息
					request.getSession().invalidate();
					request.getRequestDispatcher("index.jsp").forward(request, response);

				}
	}

	private void PgeeDoQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}
		PageData<Housers> pd = housersService.GetHousersAll(page, limit, strLike);

		JsonUi<Housers> jsonUi = new JsonUi<>(0, "", pd.getTotalCount(), pd.getData());

		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);

		out.print(string);

		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
